package cn.wolfcode.trip.admin.Controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/images")
public class ImagesController {

    /**
     *
     * 图片资源控制器
     * 返回的是七牛云的图片url
     * @param upload
     * @return
     */
    @ResponseBody
    @RequestMapping
    public Map uploadCK4(MultipartFile upload){

        Map <String, Object> map = new HashMap<>();
            try {
            //    String url = UploadUtil.upload(upload, "/upload");
                String url = UploadUtil.uploadQiniu(upload);
                map.put("url",url);
                map.put("uploaded",1);
            } catch (Exception e) {
                map.put("uploaded",0);
                Map<String,String> message =new HashMap<>();
                message.put("message","上传失败");
                map.put("error","message");
                e.printStackTrace();
            }


        return map;

    }

    /**
     * 超文本编辑器图片上传专用
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping("/xx")
    public Map upload(MultipartFile file){

        Map <String, Object> map = new HashMap<>();

        String upload = null;
        try {
          //  upload = UploadUtil.upload(file, "/upload");
            upload = UploadUtil.uploadQiniu(file);
            map.put("url",upload);
            map.put("status",1);
        } catch (Exception e) {
            map.put("status",0);
            map.put("msg","服务器空间不足");
            e.printStackTrace();
        }
        return map;

    }

}
